// success.js
const util = require('../../utils/util.js')

Page({
  data:{
    bool:false,
    codeImg: '',//图片地址
    codeNum:"",//验证码
    userAgree: 1,//用户是否同意协议
    phoneNum: '',//用户手机号
    customerid: '',//客服id
    customernum:'',//客服微信号
    option:'',//页面传值信息
    imgcookie:'',//验证码每次请求的cookie
    radioItems: [
      // { value: 'M1453145359', name: '迅维品客服1-1组', customerid: '12' },
    ],
  },
  onLoad: function (option) {
    console.log(option)
    this.setData({
      option: option
    })
    this.getAjax(); //查询客服
    this.getImg();  //  查询图片校验码
  },
  radioChange: function (e) {
    var that = this;
    var checked = e.detail.value
    var changed = {}
    for (var i = 0; i < this.data.radioItems.length; i++) {
      if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
        changed['radioItems[' + i + '].checked'] = true
        this.setData({
          customerid: this.data.radioItems[i].customerid,
          customernum: this.data.radioItems[i].value,
          bool:true
        })
      } else {
        changed['radioItems[' + i + '].checked'] = false
      }
    }
    this.setData(changed)    
  },
  getAjax:function(){
    var that =this;
    // console.log('获取客服列表')
    var sendDatas={};
   
    util.getData('user/findCustomerList', sendDatas, '123').then(function (res) {
      // console.log(res.data.data)
      const _data = res.data.data.customerList;
      var arrlists = [];
      // console.log(arrlists)
      for (var i = 0; i < _data.length; i++) {
        var str = {};
        str.value = _data[i].customer_num;  //编号
        str.name = _data[i].customer_name;  //名称
        str.customerid = _data[i].customer_id; //id
        arrlists.push(str)
      }
      that.setData({
        radioItems: arrlists,
        customerid: arrlists[0].customerid
      })
    })
  },
  codeNumEvn: function (e) {//验证码
    // console.log(e)
    this.setData({
      codeNum: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },
  phoneNumEvn: function (e) {//验证手机号
    this.setData({
      phoneNum: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
  },
  changeImgs: function () {//改变图片
    this.getImg()
  },
  userAgreeEvn: function (e) {//是否同意
    
    if (this.data.userAgree ==1){
      this.setData({
        userAgree:0
      })
    }else{
      this.setData({
        userAgree: 1
      })
    }
  },
  getImg: function () {//获取验证码的程序
    var that = this;
    // console.log('验证码')
    // wx.request({
    //   url: 'https://www4.saivineat.com/xwp/services/user/validateCode',
    //   data: {},
    //   method: 'POST',
    //   // xhrFields: { withCredentials: true },
    //   success: function (res) {
    //     console.log(res.data)
    //     if (res) {
    //        that.setData({
    //          codeImg: 'data:image/jpeg;base64,'+res.data.data.base64,
    //          imgcookie: res.data.data.sessionid
    //         }); 
    //      }
    //     console.log(that.data.imgcookie)
    //   }
    // })
    
    util.getData('user/validateCode',{},'123').then(function(res){
      // console.log(res.data.data)
         if(res){
           that.setData({
             codeImg: 'data:image/jpeg;base64,'+res.data.data.base64,
             imgcookie: res.data.data.sessionid
            }); 
         }
    });
    
  },
  checked:function(){
    var myreg = /^[1][3,4,5,7,8,6][0-9]{9}$/;
    if (!myreg.test(this.data.phoneNum)){
      wx.showModal({
        title: "提示",
        content: "请填写正确的手机号",
        showCancel: false,
        confirmText: "好的"
      })
      return false;
    } else if (this.data.codeNum == ''){
      wx.showModal({
        title: "提示",
        content: "请填写验证码",
        showCancel: false,
        confirmText: "好的"
      })
      return false;
    } else if (this.data.userAgree == 0){
      wx.showModal({
        title: "提示",
        content: "请勾选同意注册协议",
        showCancel: false,
        confirmText: "好的"
      })
      return false;
    } else if (!this.data.bool) {
      wx.showModal({
        title: "提示",
        content: "请勾选微信客服",
        showCancel: false,
        confirmText: "好的"
      })
      return false;
    }else{
      return true
    }
  },
  quitbtn:function(){
    var that = this;
    // console.log(this.data.option)
    
    var postmessage={
      open_id: wx.getStorageSync("openId"), //用户openID
      phone: this.data.phoneNum,//用户电话
      belong_coutomer: this.data.customerid,//专属客服id
      code: this.data.codeNum//验证码
    }
    console.log(this.data.imgcookie)
    if(this.checked()){
      // console.log('确认完善信息')
      wx.request({
        url: 'https://www4.saivineat.com/xwp/services/user/updateUserInfo',
        data: JSON.stringify(postmessage) ,
        method: 'POST',
        header: { 'Cookie': 'JSESSIONID=' + that.data.imgcookie },
        // xhrFields: { withCredentials: true },
        success: function (res) {
          // console.log(res.data)
          if (res.data.status == 1) {
            wx.showModal({
              title: "完善成功",
              content: "您的专属客服微信号为:" + that.data.customernum + "; 您的唯一ID码为:" + wx.getStorageSync("nack_id"),
              showCancel: false,
              confirmText: "好的",
              success: function (res){
                console.log(res);
                wx.setStorageSync("customernum", that.data.customernum)
                wx.setStorageSync("customerid", that.data.customerid)
                wx.setStorageSync("userphone", that.data.phoneNum)
                if (that.data.option.form == 1) {
                  wx.reLaunch({
                    url: '../index/index'
                  })
                } else {
                  wx.redirectTo({
                    url: '../integral_detail/integral_detail?proid=' + that.data.option.proid
                  })
                }
              }
            })
            // wx.showToast({
            //   title: "提交信息成功",
            //   icon: "success",
            //   duration: 2000
            // })
           
          }else if (res.data.status == 2) {
            wx.showToast({
              title: "验证码错误",
              icon: "none",
              duration: 2000
            })
            that.getImg()
            
          } else {
            wx.showToast({
              title: "修改失败",
              icon: "none",
              duration: 2000
            })
            that.getImg()
          }  
        }
      })
      // util.getData('user/updateUserInfo', postmessage, true).then(function (res) {
      //   console.log(res.data)
      //   if (res.data.status ==0){
      //     wx.showToast({
      //       title: "修改失败",
      //       icon: "none",
      //       duration: 2000
      //     })
      //     that.getImg()
      //   } else if (res.data.status == 2){
      //     wx.showToast({
      //       title: "验证码错误",
      //       icon: "none",
      //       duration: 2000
      //     })
      //     that.getImg()
      //   } else {
      //     wx.showToast({
      //       title: "提交信息成功",
      //       icon: "success",
      //       duration: 2000
      //     })
      //     if (this.data.option.form ==1){
      //       wx.navigateTo({
      //         url: '../index/index'
      //       })
      //     }else{
      //       wx.navigateTo({
      //         url: '../integral_detail/integral_detail?proid=' + this.data.option.proid
      //       })
      //     }
      //   }
      // })
    }
    
  }
})